#include "image.h"


uint8 image[MT9V03X_H][MT9V03X_W];        // er zhi hua tuxiang 
uint8 base_image[MT9V03X_H][MT9V03X_W];   // yuan shi tuxiang
uint8 left_jidian;                        //zuo jidian
uint8 right_jidian;                       //you jidian
uint8 left_line_list[MT9V03X_H];          //zuo shuzu
uint8 right_line_list[MT9V03X_H];         //you shuzu
uint8 mid_line_list[MT9V03X_H];
uint8 mid_line[MT9V03X_H];                //zhong xian shuzu 
 
//---------------------------------da jin fa bian liang-------------------------------
#define GrayScale 256
#define grayscale 256
uint16 hist[GrayScale]={0};     // hui du zhi xiang su dian shu liang
float P[GrayScale]={0};         //hui du zhi gailv
float PK[GrayScale]={0};        //galv lei ji he
float MK[GrayScale]={0};        //hui du zhi lei jia jun zhi
uint8 img_threshold;            //shu chu yu zhi
float imgsize;                  //xiangsu zonghe


uint8 Ostu(uint8 index[MT9V03X_H][MT9V03X_W])
{
  uint8 threshold;
	imgsize = MT9V03X_W * MT9V03X_H;   //huoqu zong xaingsu
	uint8 images_value_temp;           //zhong jian bian liang 
	 
	float sumPK = 0;
	float sumMK = 0;
	float var = 0;
	float vartmp = 0;
	
	//clean
	for(uint16 i = 0; i<GrayScale; i++)
	{
	    hist[i]=0;
			P[i]=0;
			PK[i]=0;
			MK[i]=0;
	}
	// find lei jian fangcha 
	for(uint16 i = 0; i < GrayScale; i++)
	{
	    P[i]=(float)hist[i]/imgsize;
		PK[i]=sumPK+P[i];
		sumPK=PK[i];
		MK[i]=sumMK+i*P[i];
		sumMK=MK[i];
	}
	//fine max yuzhi
	for(uint8 i =5;i<245;i++)
	{
	  vartmp=((MK[GrayScale-1]*PK[i]-MK[i])*(MK[GrayScale-1]*PK[i]-MK[i]))/(PK[i]*(1-PK[i]));
		if(vartmp>var)
		{
		  var =vartmp;
			threshold = i;//shu chu yuzhi
		}
	}
  return threshold;
}


//--------------------------fen duan er zhi hua----------------------
void set_image_twovalues(uint8 value)
{
	uint8 temp_value;
	for(uint8 i=0;i<40;i++)
	{
		for(uint8 j=0;j<MT9V03X_W;j++)
		{
			temp_value=base_image[i][j];
			if(temp_value<value+20)  //yuan duan er zhi hua
			{
				image[i][j]=0;
			}
			else
			{
				image[i][j]=255;
			}
		}
	}
	
	for(uint8 i=40;i<MT9V03X_H;i++)		
	{
    for(uint8 j=0;j<MT9V03X_W;j++)
    {
			temp_value=base_image[i][j];
			if(temp_value<value)  // jin duan er zhi hua 
			{
				image[i][j]=0;
			}
		  else
			{
			  image[i][j]=255;
			}
		}		
	}	
}


//---------------------find ji dian---------------------
void find_jidian(uint8 index[MT9V03X_H][MT9V03X_W])
{
	if(index[jidian_search_line -1 ][MT9V03X_W/2]==255 && index[jidian_search_line-1][MT9V03X_W/2+1]==255 && index[jidian_search_line-1][MT9V03X_W/2-1]==255)
	{
		// find zuo jidian
    for(uint8 j = MT9V03X_W/2;j>0;j--)  //cong zhongjian xiang zuobian sao xian 
		{
			if(index[jidian_search_line-1][j-1]==0 && index[jidian_search_line-1][j]==255 && index[jidian_search_line-1][j+1]==255)  // 255 is white , 0 is black
			{
				left_jidian = j;
				break;
			
			}
			if(j-1==1)
			{
				left_jidian=1;
				break;
		  }
  	}
		//fine you jidian
		for(uint8 j = MT9V03X_W/2;j<MT9V03X_W-2;j++)  //cong zhongjian xiang youbian sao xian 
		{
			if(index[jidian_search_line-1][j-1]==255 && index[jidian_search_line-1][j]==255 && index[jidian_search_line-1][j+1]==0)  // 255 is white , 0 is black
			{
				right_jidian = j;
				break;
			
			}
			if(j-1==1)
			{
				right_jidian=1;
				break;
		  }
  	}
  }
}


//-----------find saidao bian xian--------------
void image_deal(uint8 index[MT9V03X_H][MT9V03X_W])
{
	//let frist search base on jidian
	uint8 left_point = left_jidian;
	uint8 right_point = right_jidian;

	for(uint8 i = search_start_line -1 ; i>search_end_line; i--)
	{
		uint8 left_search_judge=0;
		uint8 mid_start_left_search_judge=0;
	//find zuo bianjie dian 
			for(uint8 j = left_point; j<left_point+left_line_right_search; j++)
			{
				if(index[i][j-1]==0 && index[i][j]==255 && index[i][j+1]==255)
				{
					left_point = j;
					break;
				}
				else if(j==MT9V03X_W-2)
				{
					left_point = MT9V03X_W-5;
					break;
				}
				else if(j==left_point +left_line_right_search-1)
				{
				  left_search_judge = 1;
					break;
				
				}
			}
			//zuo fine 5 line
			if(left_search_judge == 1)
			{
			  for(uint8 j = left_point;j>left_point-left_line_left_search;j--)
				{
					if(index[i][j-1]==0 && index[i][j]==255 && index[i][j+1]==255 && j<MT9V03X_W-5)
					{
						left_point = j;
						break;
					}
					else if(j == 1)
					{
						left_point =1 ;
						mid_start_left_search_judge = 1;
						break;
					
					}
				  else if(j == left_point - left_line_left_search+1)
					{
					   mid_start_left_search_judge = 1;
						 break;
					}
				}
			}
			//cong zhongjian wang zuo zhao
			if(mid_start_left_search_judge==1)
			{
				for(uint8 j= MID_W; j>0; j--)
				{
					if(index[i][j-1]==0 && index[i][j]==255 && index[i][j+1]==255 )
					{
						left_point=j;
						break;
					}
					else if(j==1)
					{
						left_point=1;
						break;
					}
				}
			}
			
		uint8 right_search_judge=0;
		uint8 mid_start_right_search_judge=0;
	//find you bianjie dian 
	
			for(uint8 j = right_point; j>right_point+right_line_left_search; j++)
			{
				if(index[i][j-1]==255 && index[i][j]==255 && index[i][j+1]==0)
				{
					right_point = j;
					break;
				}
				else if(j==1)
				{
					right_point = 4;
					break;
				}
				else if(j==right_point +right_line_left_search+1)
				{
				  right_search_judge = 1;
					break;
				
				}
			}
			//you fine 5 line
			if(right_search_judge == 1)
			{
			  for(uint8 j = right_point;j<right_point+right_line_right_search;j++)
				{
					if(index[i][j-1]==255 && index[i][j]==255 && index[i][j+1]==0 && j>4)
					{
						right_point = j;
						break;
					}
					else if(j == MT9V03X_W-2)
					{
						right_point =MT9V03X_W-2 ;
						mid_start_right_search_judge = 1;
						break;
					
					}
				  else if(j == right_point + right_line_right_search-1)
					{
					   mid_start_right_search_judge = 1;
						 break;
					}
				}
			}
			//cong zhongjian wang you zhao
			if(mid_start_right_search_judge==1)
			{
				for(uint8 j= MID_W; j<MT9V03X_W-1; j++)
			  {
					if(index[i][j-1]==255 && index[i][j]==255 && index[i][j+1]==0 )
					{
						right_point=j;
						break;
					}
					else if(j==MT9V03X_W-2)
					{
						right_point=MT9V03X_W-2;
						break;
					}
				}
			}
	    left_line_list[i]=Limit_uint8(1,left_point,MT9V03X_W-2);
			right_line_list[i]=Limit_uint8(1,right_point,MT9V03X_W-2);
			mid_line_list[i]=Limit_uint8(1,(left_point+right_point)/2,MT9V03X_W-2);
		
		}
}

//----------tu xiang zhongxian quanzhong--------------------------

uint8 mid_weight_list[120]=
{
	1,1,1,1,1,1,1,1,1,1,
	1,1,1,1,1,1,1,1,1,1,
	1,1,1,1,1,1,1,1,1,1,
	1,1,1,1,1,1,1,1,1,1,
	1,1,1,1,1,1,1,1,1,1,
	6,6,6,6,6,6,6,6,6,6,
	7,8,9,10,11,12,13,14,15,16,
	17,18,19,20,20,20,20,19,18,17,
	16,15,14,13,12,11,10,9,8,7,
	6,6,6,6,6,6,6,6,6,6,
  1,1,1,1,1,1,1,1,1,1,
	1,1,1,1,1,1,1,1,1,1,
};



uint8 final_mid_line=MID_W; 
uint8 last_mid_line=MID_W;

uint8 find_mid_line_weight(void)
{
	uint8 mid_line_value=MID_W;  //zui zhong
	uint8 mid_line=MID_W;        //ben ci zhong xian
	uint32 weight_midline_sum=0; //jia quan lei ji
	uint32 weight_sum =  0;      //quanzhong lei jia  
	
	
	for(uint8 i = MT9V03X_H-1;i>search_end_line;i--)
	{
		weight_midline_sum +=mid_line_list[i]*mid_weight_list[i];
		weight_sum+=mid_weight_list[i];
	}
	mid_line=(uint8)(weight_midline_sum/weight_sum);
	mid_line_value=last_mid_line*0.2+mid_line*0.8;  //lv bo
	last_mid_line=mid_line_value;
	return mid_line_value;

}




